/* -+- file  ./kf2/GUI/NewMessage.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;
using Habazie.kf2.Net;

namespace Habazie.kf2.GUI
{
	
	public class NewMessage : Gtk.Window
	{
		private App m_App;
		private Account m_Account = null;
		protected Gtk.Entry m_To;
		protected Gtk.Entry m_Subject;
		protected Gtk.TextView m_Body;
		protected Habazie.kf2.GUI.AccountSelector m_AccountSelector;

		public NewMessage (Account acc) : this ()
		{
			ThisAccount = acc;
		}
		
		public NewMessage() : 
				base("")
		{
			m_App = Habazie.kf2.Core.Util.Application;
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.NewMessage));
			m_AccountSelector.Connections = m_App.Connections;
		}
		
		public string To {
			set { m_To.Text = value; }
		}
		
		public Account ThisAccount {
			set {
				m_Account = value;
				m_AccountSelector.Account = value;
			}
			get {
				return m_AccountSelector.Account;
			}
		}

		protected virtual void OnSendActivated(object sender, System.EventArgs e)
		{
			Account acc = m_Account;
			if (acc == null)
				acc = m_AccountSelector.Account;
			if (acc == null)
				return;
				
			string to = m_To.Text;
			string subject = m_Subject.Text;
			string body = m_Body.Buffer.Text;
			
			Message msg = new Message (acc.Document);
			msg.To = new jabber.JID (to);
			msg.Subject = subject;
			msg.Body = body;
			//m_Account = m_AccountSelector.Account;
			acc.Write (msg);
			Destroy ();
		}
		
		public void Reply (jabber.protocol.client.Message msg)
		{
			To = msg.From;
			m_Subject.Text = "Re: " + msg.Subject;
			m_Body.Buffer.Text = msg.Body;
		}
		
		public void Forward (jabber.protocol.client.Message msg)
		{
//			To = msg.From;
			m_Subject.Text = "Fwd: " + msg.Subject;
			m_Body.Buffer.Text = msg.Body;
		}
	}
}
